package com.itheima;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class MapDemo {
    public static void main(String[] args) {
        test0();
        test1();
        test2();
        test3();
    }
    public static void test0(){
        Map<String,Double> map = new HashMap<>();
        map.put("zhangsan",100.0);
        map.put("lisi",100.0);
        map.put("wangwu",100.0);
        map.put("zhaoliu",100.0);
        System.out.println(map);
        System.out.println("-----------------------");
        map.remove("zhangsan");
        System.out.println(map);
        System.out.println("-----------------------");
        boolean b1 = map.containsKey("lisi");
        System.out.println("b1="+b1);
        boolean b2 = map.containsValue(100.0);
        System.out.println("b2="+b2);
        System.out.println("-----------------------");
        boolean empty = map.isEmpty();
        System.out.println("empty="+empty);
        System.out.println("-----------------------");
        int size = map.size();
        System.out.println("size="+size);
        //遍历map
        for (Map.Entry<String,Double> entry:map.entrySet()){
            System.out.println(entry.getKey()+","+entry.getValue());
        }
    }
    public static void test1() {
        Map<String, Double> map = new HashMap<>();
        map.put("张三", 100.0);
        map.put("李四", 200.0);
        map.put("王五", 300.0);
        map.put("赵六", 400.0);

        // 将map中的所有键取出来放到Set集合中
        Set<String> keys = map.keySet();
        System.out.println("----------------");
        // 遍历并打印所有键值对
        for (String key : keys) {
            System.out.println(key + ": " + map.get(key));
        }
    }
    public static void test2() {
        Map<String, Double> map = new HashMap<>();
        map.put("张三", 100.0);
        map.put("李四", 200.0);
        map.put("王五", 300.0);
        map.put("赵六", 400.0);
        System.out.println("----------------");
        for (Map.Entry<String,Double> entry:map.entrySet()){
            System.out.println(entry.getKey()+","+entry.getValue());
        }
    }
    public static void test3() {
        // 创建一个HashMap，键为String类型，值为Double类型
        Map<String, Double> map = new HashMap<>();

        // 向map中添加键值对
        map.put("张三", 100.0);
        map.put("李四", 200.0);
        map.put("王五", 300.0);
        map.put("赵六", 400.0);
        System.out.println("--------------------------");
        // 使用forEach方法遍历map中的所有键值对
        map.forEach((s, aDouble) -> {
            // 在这里处理每个键值对
            // 打印键和值
            System.out.println(s + " --- " + aDouble);
        });
    }
}
